import React from 'react';
import PropTypes from 'prop-types';
import cn from 'classnames';
import fetch from 'API/fetch'
import Button from '@kuiper-components/button';
import notification from '@kuiper-components/notification';

import confirm from '../confirm';
import s from './AddYunaqBlacklist.module.css';

export default function AddYunaqBlacklist({
	className,
	warningId,
	taskId,
	onOk,
	...props
}) {

	return (
		<Button
			primary
			className={cn(className, s.addYunaqBlacklist)}
			{...props}
			onClick={() =>
				confirm.normal({
					message:
						'该风险URL将联动云防御平台相应域名URL黑名单功能，一键加黑页面，请谨慎操作。',
					okText: '加黑',
					cancelText: '取消',
					title: '提示',
					onOk: () =>
						fetch('/api/v2/cloud/udb_add_blacklist', { method: 'POST', body: JSON.stringify({ taskId, warningId }) })
							.then(resp => {
								if (resp.code !== 200) {
									throw new Error(resp.msg);
								}
								notification.success({
									message: `添加成功`,
									placement: 'top',
								});
								onOk();
							})
							.catch(error => {
								notification.error({
									message: `添加失败`,
									description: error.message,
									placement: 'top',
								});
							}),
				})
			}
		>
			加黑风险URL
		</Button>
	);
}

AddYunaqBlacklist.propTypes = {
	className: PropTypes.string,
	warningId: PropTypes.string.isRequired,
	taskId: PropTypes.string.isRequired,
	onOk: PropTypes.func,
};

AddYunaqBlacklist.defaultProps = {
	className: s.addYunaqBlacklist,
	onOk: () => {},
};
